package org.example.managementsystem.service;

import org.example.managementsystem.model.Provider;
import org.example.managementsystem.model.ProviderComplaint;
import org.example.managementsystem.model.ProviderComplaintReview;
import org.example.managementsystem.repository.ProviderComplaintDao;
import org.example.managementsystem.repository.ProviderComplaintReviewDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 供应商投诉审核服务层
 * @author 王振祎
 * @date 2025-05-20
 */
@Service
public class ProviderComplaintReviewService {

    private final ProviderComplaintReviewDao providerComplaintReviewDao;
    private final ProviderComplaintDao providerComplaintDao;
    private final ProviderService providerService;
    @Autowired
    public ProviderComplaintReviewService(ProviderComplaintReviewDao providerComplaintReviewDao, ProviderComplaintDao providerComplaintDao, ProviderService providerService) {
        this.providerComplaintReviewDao = providerComplaintReviewDao;
        this.providerComplaintDao = providerComplaintDao;
        this.providerService = providerService;
    }

    /**
     * 审核供应商投诉
     */
    public void reviewComplaint(int complaintId, boolean isApproved, String description,int ratingDeduction) {

        // 先查询表中是否有对应id的审核记录
        ProviderComplaintReview existingReview = providerComplaintReviewDao.getReviewByComplaintId(complaintId);
        if(existingReview != null){
            // 如果有，则更新该记录
            existingReview.setApproved(isApproved);
            existingReview.setDescription(description);
            existingReview.setRatingDeduction(ratingDeduction);
            providerComplaintReviewDao.updateReview(existingReview);

            // 投诉审核更新成功后，更新投诉表中的审核状态
            providerComplaintDao.updateComplaintStatus(complaintId, isApproved);
            if(existingReview.isApproved()){
                ProviderComplaint complaint = providerComplaintDao.getComplaintById(complaintId);
                Provider provider = providerService.getProviderByName(complaint.getProviderName());
                int ratingAfter = provider.getProviderRating() - existingReview.getRatingDeduction();
                if(ratingAfter <= 0){
                    ratingAfter = 0;
                } else if (ratingAfter >= 5) {
                    ratingAfter= 5;
                }
            }

        }
        else{
            ProviderComplaintReview review = new ProviderComplaintReview();
            review.setComplaintId(complaintId);
            review.setApproved(isApproved);
            review.setDescription(description);
            review.setRatingDeduction(ratingDeduction);
            providerComplaintReviewDao.insertReview(review);

            // 投诉审核更新成功后，更新投诉表中的审核状态
            providerComplaintDao.updateComplaintStatus(complaintId, isApproved);

            // 审批通过后，更新供应商表中的评分
            if(review.isApproved()){
                ProviderComplaint complaint = providerComplaintDao.getComplaintById(complaintId);
                Provider provider = providerService.getProviderByName(complaint.getProviderName());
                int ratingAfter = provider.getProviderRating() - review.getRatingDeduction();
                if(ratingAfter <= 0){ratingAfter= 0;}
                if(ratingAfter >= 5){
                    ratingAfter = 5;
                }
                provider.setProviderRating(ratingAfter);
                providerService.updateProvider(provider);
            }
        }


    }

    public void reviewComplaint(ProviderComplaintReview review) {

        // 先查询表中是否有对应id的审核记录
        ProviderComplaintReview existingReview = providerComplaintReviewDao.getReviewByComplaintId(review.getComplaintId());

        if(existingReview != null){
            // 如果有，则更新该记录
            existingReview.setApproved(review.isApproved());
            existingReview.setDescription(review.getDescription());
            existingReview.setRatingDeduction(review.getRatingDeduction());
            providerComplaintReviewDao.updateReview(existingReview);

            // 投诉审核更新成功后，更新投诉表中的审核状态
            providerComplaintDao.updateComplaintStatus(review.getComplaintId(), review.isApproved(), true);
            if(existingReview.isApproved()){
                ProviderComplaint complaint = providerComplaintDao.getComplaintById(review.getComplaintId());
                Provider provider = providerService.getProviderByName(complaint.getProviderName());
                int ratingAfter = provider.getProviderRating() - existingReview.getRatingDeduction();
                if(ratingAfter <= 0){
                    ratingAfter = 0;
                } else if (ratingAfter >= 5) {
                    ratingAfter= 5;
                }
                provider.setProviderRating(ratingAfter);
                providerService.updateProvider(provider);
            }
        }
        else {
            providerComplaintReviewDao.insertReview(review);

            // 投诉审核更新成功后，更新投诉表中的审核状态
            providerComplaintDao.updateComplaintStatus(review.getComplaintId(), review.isApproved(), true);

            // 审批通过后，更新供应商表中的评分
            if(review.isApproved()){
                ProviderComplaint complaint = providerComplaintDao.getComplaintById(review.getComplaintId());
                Provider provider = providerService.getProviderByName(complaint.getProviderName());
                int ratingAfter = provider.getProviderRating() - review.getRatingDeduction();
                if(ratingAfter <= 0){
                    ratingAfter = 0;
                } else if (ratingAfter >= 5) {
                    ratingAfter = 5;
                }
                provider.setProviderRating(ratingAfter);
                providerService.updateProvider(provider);
            }
        }

    }

    /**
     * 更新投诉审核记录
     */
    public void updateReview(ProviderComplaintReview review) {
        providerComplaintReviewDao.updateReview(review);
    }

    /**
     * 获取所有历史投诉审核记录
     */
    public List<ProviderComplaintReview> getAllReviews() {
        return providerComplaintReviewDao.getAllReviews();
    }

    /**
     * 根据ID获取投诉审核记录
     */
    public ProviderComplaintReview getReviewById(int complaintId) {
        return providerComplaintReviewDao.getReviewByComplaintId(complaintId);
    }

}
